/**
 * 违章条款奖惩规则 列表页
 *
 * 作者 admin
 * 日期 2024-08-08 13:20:28
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-peccancyClauseRule'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#peccancyClauseRule-table-form'),
        leftTableIns,
        rightTableIns;

    let peccancyClauseRuleTableQueryParams = {
        tenantId:null,
        clauseId:null,
        clauseName:null,
        clauseNo:null,
        status:null,
        disposeForm:null
    } ;
    form.verify(validate);
    form.render();

    $view.find('#selectPeccancyClause').on("click",function (){
        febs.modal.open('选择违章条款', 'common/peccancyClause', {
            btn: ['确认','关闭'],
            area: $(window).width() <= 750 ? ['95%', '85%']: ['70%', '85%'],
            yes: function (index, layero) {
                getSelectData();
                layer.close(index);
            },
            btn2: function (index, layero) {
                layer.close(index);
                return false;
            }
        });
    })

    initTable();

    function initTable() {
        initLeftTable();
    }

    $query.on('click', function () {
        //重载左侧表格
        leftTableIns.reload({where: getQueryParams(), page: {curr: 1}});
    });

    table.on('tool(peccancyClauseTable)', function (obj) {
        //表格编辑删除事件
        var id = obj.data.clauseId;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            febs.modal.open('修改违章条款类型', 'peccancyClause/update/'+id, {
                btn: ['提交', '取消'],
                area: $(window).width() <= 750 ? '90%' : ['40%','40%'],
                yes: function (index, layero) {
                    $('#peccancyClause-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
            });
            $view.find('#ruleQuery').trigger('click');
        } else if (layEvent === 'del') {
            febs.modal.confirm('删除条款', '确定删除所选条款？', function () {
                deleteIds(id);
                $query.trigger('click');
            });
        }else if (layEvent === 'set') {
            peccancyClauseRuleTableQueryParams.tenantId = obj.data.tenantId
            peccancyClauseRuleTableQueryParams.clauseId = obj.data.clauseId
            peccancyClauseRuleTableQueryParams.clauseName = obj.data.clauseName
            peccancyClauseRuleTableQueryParams.status = $view.find("select[name='ruleStatus']").find("option:selected").val();
            initRightTable();
        }

    });

    table.on('toolbar(peccancyClauseTable)', function (obj) {
        if (obj.event === 'add') {
            febs.modal.open('新增违章条款类型', 'peccancyClause/add', {
                btn: ['提交','取消'],
                area: [$(window).width() <= 750 ? '90%' : '40%','40%'],
                yes: function (index, layero) {
                    $('#peccancyClause-edit').find('#submit').trigger('click');
                },
                data:{
                    tenantId: $searchForm.find('select[name="tenantId"]').val()
                }
            });
        }
    });



    table.on('tool(peccancyClauseRuleTable)', function (obj) {
        //表格编辑删除事件
        var id = obj.data.clauseRuleId;
        var layEvent = obj.event;
        if (layEvent === 'edit') {
            febs.modal.open('修改条款类型规则', 'peccancyClauseRule/update/'+id, {
                btn: ['提交', '取消'],
                area: [$(window).width() <= 750 ? '90%' : '50%','60%'],
                yes: function (index, layero) {
                    $('#peccancyClauseRule-edit').find('#submit').trigger('click');
                },
                btn2: function () {
                    layer.closeAll();
                },
            });
            $view.find('#ruleQuery').trigger('click');
        } else if (layEvent === 'del') {
            febs.modal.confirm('删除条款', '确定删除所选条款？', function () {
                deleteRuleIds(id);
                $view.find('#ruleQuery').trigger('click');
            });
        }

    });

    table.on('toolbar(peccancyClauseRuleTable)', function (obj) {
        if (obj.event === 'ruleQuery') {
            queryPeccancyClauseRule(obj)
        }else if  (obj.event === 'ruleAdd') {
            addPeccancyClauseRule(obj)
        }
    });

    form.on('switch(statusOption)', function (obj) {
        //奖品状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其id
        var id =  table.cache['peccancyClauseTable'][index].clauseId;

        febs.modal.confirm('更改条款状态', '确认更改条款状态',function (){
            updateStatus(id,status);
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });
    function updateStatus(id,status) {
        //更新奖品状态
        let model={
            clauseId : id,
            status : status
        }
        febs.postArray(ctx + 'peccancyClause/updateStatus', model, function () {
            febs.alert.success('修改条款状态成功');
        })
    }

    form.on('switch(statusOptionRule)', function (obj) {
        //奖品状态控制点击事件
        var checked = obj.elem.checked;
        var status;
        if (checked) {
            status = "1";
        } else {
            status = "0";
        }
        //获取该控件所在行
        var index = obj.othis.parents('tr').eq(0).data('index');
        //获取表中所有数据 按行取数据及其id
        var id =  table.cache['peccancyClauseRuleTable'][index].clauseRuleId;

        febs.modal.confirm('更改奖惩状态', '确认更改奖惩状态',function (){
            updateRuleStatus(id,status);
        },function (){
            obj.elem.checked = !checked;
            layer.closeAll();
            form.render('checkbox');
        });

    });

    function updateRuleStatus(id,status) {
        //更新奖品状态
        let model={
            clauseRuleId : id,
            status : status
        }
        febs.postArray(ctx + 'peccancyClauseRule/updateStatus', model, function () {
            febs.alert.success('修改改奖状态成功');
        })
    }



    function getQueryParams() {
        return {
            tenantId: $searchForm.find('select[name="tenantId"]').val(),
            status: $searchForm.find("select[name='status']").find("option:selected").val(),
            useScope: $searchForm.find("select[name='useScope']").find("option:selected").val(),
            clauseName: $searchForm.find("input[name='clauseName']").val(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'peccancyClause/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }

    function deleteRuleIds(ids) {
        febs.get(ctx + 'peccancyClauseRule/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        })
    }
    function initLeftTable(){
        leftTableIns = febs.table.init({
            elem: $view.find('table[name="peccancyClauseTable"]'),
            id: 'peccancyClauseTable',
            url: ctx + 'peccancyClause/list',
            where: getQueryParams(),
            height: getTableHeight(),
            skin: '',
            cols: [[
                {type: 'numbers',width: 60, title: '序号'},
                {field: 'clauseId', title: 'id', minWidth: 120,hide:true},
                {field: 'tenantId', title: '公司ID', minWidth: 120,hide:true},
                {field: 'clauseName', title: '条款名称', minWidth: 140},
                {field: 'useScope', title: '适用范围',align:'center', width: 90,templet:function (d) {
                        if(d.useScope == '1'){
                            return  '<span class="layui-badge febs-tag-cyan">本工</span>'
                        }else if(d.useScope == '2'){
                            return  '<span class="layui-badge febs-tag-orange">外包</span>'
                        }else if(d.useScope == '3'){
                            return  '<span class="layui-badge febs-tag-green">外协</span>'
                        }else if(d.useScope == '4'){
                            return  '<span class="layui-badge febs-tag-geekblue">承包队</span>'
                        }else if(d.useScope == '10'){
                            return  '<span class="layui-badge febs-bg-blue">全部</span>'
                        }
                    }},
                {field: 'sort', title: '排序',align:'center', width: 40},
                {field: 'status', title: '状态',toolbar: '#statusOption' ,align:'center', width: 95},
                {title: '操作', toolbar: '#peccancyClause-option',align:'center', width: 215,fixed:'right'}

            ]],
            toolbar: '#peccancyClause-toolbar',
            defaultToolbar: [],
            done: function (res, curr, count) {
                if(res && res.count > 0 ){
                    $view.find(".layui-table-view[lay-table-id='peccancyClauseTable'] .layui-table-body.layui-table-main tr[data-index=0] button[lay-event='set']").click();
                }else{
                    peccancyClauseRuleTableQueryParams.tenantId = $searchForm.find('select[name="tenantId"]').val()
                    peccancyClauseRuleTableQueryParams.status = $searchForm.find("select[name='status']").find("option:selected").val()
                    peccancyClauseRuleTableQueryParams.useScope = $searchForm.find("select[name='useScope']").find("option:selected").val()
                    initRightTable()
                    $view.find('#ruleAdd').attr("disabled",true).addClass("layui-btn-disabled");
                    $view.find('#ruleQuery').attr("disabled",true).addClass("layui-btn-disabled");
                }
            }
        });
    }

    function initRightTable(){
        rightTableIns = febs.table.init({
            elem: $view.find('table[name="peccancyClauseRuleTable"]'),
            id: 'peccancyClauseRuleTable',
            height: getTableHeight(),
            skin: '',
            where:peccancyClauseRuleTableQueryParams,
            url: ctx + 'peccancyClauseRule/listByClauseId',
            cols: [[
                {type: 'numbers',width: 60, title: '序号'},
                {field: 'clauseRuleId', title: 'id', minWidth: 120,hide:true},
                {field: 'tenantId', title: '公司ID', minWidth: 120,hide:true},
                {field: 'clauseId', title: '违章条款Id', minWidth: 120,hide:true},
                {field: 'clauseNo', title: '条款编号', width: 120},
                {field: 'clauseLevel', title: '条款等级', width: 90,align:'center'},
                {field: 'disposeForm', title: '处理形式', width: 200,templet:function (d){
                        if(d.disposeForm){
                            return febs.getDictLabel(disposeFormList,d.disposeForm)
                        }
                 }},
                {field: 'referenceMoney', title: '罚款金额', width: 120,align:'center'},
                {field: 'examineScore', title: '考核分数', width: 90,align:'center'},
                {field: 'clauseContent', title: '条款内容', minWidth: 120},
                {field: 'executeDate', title: '执行时间', width: 110,align:'center'},
                {field: 'status', title: '状态',toolbar: '#statusOptionRule' ,align:'center', width: 120},
                {title: '操作', toolbar: '#peccancyClauseRule-option',align:'center', width: 150,fixed:'right'}
            ]],
            toolbar: '#peccancyClauseRule-toolbar',
            defaultToolbar: [],
            done: function (res, curr, count) {
                if(!commonJS.isEmpty(peccancyClauseRuleTableQueryParams.clauseNo)){
                    $view.find('input[name="clauseNo"]').val(peccancyClauseRuleTableQueryParams.clauseNo)
                }
            }
        });
    }

    function  queryPeccancyClauseRule (obj){
        if(!commonJS.isEmpty(peccancyClauseRuleTableQueryParams.clauseId)){
            peccancyClauseRuleTableQueryParams.clauseNo = $view.find('input[name="clauseNo"]').val();
            peccancyClauseRuleTableQueryParams.disposeForm =  $view.find("select[name='disposeForm']").find("option:selected").val();
            peccancyClauseRuleTableQueryParams.status = $view.find("select[name='ruleStatus']").find("option:selected").val();
            //重载右侧承包队表
            initRightTable();
        }else {
            febs.alert.error('未选中条款类型。');

        }
    }

    function addPeccancyClauseRule(obj){
        if(!commonJS.isEmpty(peccancyClauseRuleTableQueryParams.clauseId)){
            peccancyClauseRuleTableQueryParams.clauseNo = $view.find('input[name="clauseNo"]').val();
            peccancyClauseRuleTableQueryParams.disposeForm =  $view.find("select[name='disposeForm']").find("option:selected").val();
            peccancyClauseRuleTableQueryParams.status = $view.find("select[name='ruleStatus']").find("option:selected").val();
            //重载右侧承包队表
            initRightTable();
        }else {
            febs.alert.error('未选中条款类型。');
            return;
        }
        febs.modal.open('新增条款类型规则', 'peccancyClauseRule/add', {
            btn: ['提交','取消'],
            area: [$(window).width() <= 750 ? '90%' : '50%','60%'],
            yes: function (index, layero) {
                $('#peccancyClauseRule-edit').find('#submit').trigger('click');
            },
            data:{
                tenantId :  peccancyClauseRuleTableQueryParams.tenantId,
                clauseId: peccancyClauseRuleTableQueryParams.clauseId,
                clauseName :  peccancyClauseRuleTableQueryParams.clauseName
            }
        });
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        leftTableIns.reload({height: getTableHeight()});
        rightTableIns.reload({height: getTableHeight()});
    });

});